package com.jvm.classloader.custom;

import org.apache.commons.lang3.StringUtils;

import java.beans.Encoder;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * ClassLoader..
 *
 * @author Lizhong
 * @date 2019/7/28
 */
public class App {
    static CustomClassLoader02 urlClassLoader = null;
    static {
        String s = System.getProperty("java.ext.dirs");
        String userDir = System.getProperty("user.dir");
        // jdk9 没有该属性
        if (StringUtils.isEmpty(s)) s = "D:\\Java\\jdk-11.0.1\\lib\\";
        URL ext = null;
        URL url = null;
        try {
            ext = new File(s).toURI().toURL();
            File file = new File(userDir+"\\java-se\\src\\main\\java\\com\\jvm\\classloader\\custom\\");
            url = file.toURI().toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        URL[] url1 = {url, ext};
        urlClassLoader = new CustomClassLoader02(url1);
    }

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        final ClassLoader classLoader = App.class.getClassLoader();
        System.out.println("main"+classLoader);
        BeforeJDK9();

    }

    /**
     * jdk9之前运行才会得到对应的结果
     * 将jave.jar放在 jre/lib/ext/
     * @throws ClassNotFoundException
     */
    private static void BeforeJDK9() throws ClassNotFoundException {

        //
        final ClassLoader classLoader2 = System.class.getClassLoader();
        System.out.println("System.class :"+classLoader2);

        final String properties = System.getProperty("java.class.path");
        System.out.println("java.class.path:"+properties);
        // 隐式调用，
        Encoder encoder = new Encoder();
        ClassLoader classLoader1 = encoder.getClass().getClassLoader();
        System.out.println(classLoader1); /// AppClassLoader
        // 系统类加载器
        System.out.println("String,class: " +String.class.getClassLoader()); // null

        // ExtClassLoader
        Class<?> aClass = urlClassLoader.loadClass("it.sauronsoftware.jave.Encoder");
        ClassLoader classLoader = aClass.getClassLoader();
        System.out.println(classLoader); // ExtClassLoader
        // 自定义加载器
        Class<?> aClass1 = urlClassLoader.loadClass("LoaderSample");
        Object o = aClass1.getClassLoader();
        System.out.println(o);  // CustomClassLoader02
    }

    /**
     * 将jave.jar放在 lib/
     * jdk9之后的调用
     * @throws ClassNotFoundException
     */
    private static void JDK9() throws ClassNotFoundException {
        // 隐式调用，
        Encoder encoder = new Encoder();
        ClassLoader classLoader1 = encoder.getClass().getClassLoader();
        System.out.println(classLoader1); /// AppClassLoader

        System.out.println("Object,class: " +String.class.getClassLoader()); // null


        Class<?> aClass = urlClassLoader.loadClass("it.sauronsoftware.jave.Encoder");
        ClassLoader classLoader = aClass.getClassLoader();
        System.out.println(classLoader); // AppClassLoader


        Class<?> aClass1 = urlClassLoader.loadClass("LoaderSample");
        Object o = aClass1.getClassLoader();
        System.out.println(o);  // CustomClassLoader02
    }
}
